import 'dart:convert';

import 'package:bot_toast/bot_toast.dart';
import 'package:exams_app/login_module/real_name_authen/view.dart';
import 'package:exams_app/net/Address.dart';
import 'package:exams_app/net/DioManager.dart';
import 'package:exams_app/net/persisten_storage.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';

class AccountLogic extends GetxController{




  // var imagePath = ''.obs;

  // RxMap json;

  // var json = {}.obs;

  Rx<RenZhenInfoModel> rzmodel;

  var userJson = {}.obs;

  var imageHeader = ''.obs;



  // RxMap userJson;

  /// 获取用户个人信息
  Future requestDataWithUserInfo() async {
    var params = {
      'access_token': await PersistentStorage().getStorage('acctoken'),
      'uid': await PersistentStorage().getStorage('uid'),
      'partner_id': await PersistentStorage().getStorage('partner_id'),
      'model': 'res.partner',
      'fields': 'name,full_name,sfz,sfz_heads,sfz_tails,ai_face,exam_project_name,exam_company_name',
      // 'domain': "[('user_id', '=', ${await PersistentStorage().getStorage('uid')})]",  //#我的表达式
    };
    String result = await DioUtils(
        url: await PersistentStorage().getStorage('urlStr'))
        .post(
        '/api/v1/read/${await PersistentStorage().getStorage('partner_id')}',
        data: params);
    Map _json = jsonDecode(result);

    rzmodel = RenZhenInfoModel.fromJson(_json).obs;


    // json = _json.obs;

    update();


    return rzmodel;

  }


  /// 获取账号信息 _api_my_name
   requestDataWithUser()async{


    var  params = {
      // 'order': 'top_time desc, id desc',
    };

    String result = await DioUtils(url: await PersistentStorage().getStorage('urlStr')).
    postModel(Address.getattt,function_name:'_api_my_name',model:'res.users',params:null );
    Map _json = jsonDecode(result);

    userJson = _json.obs;

    if(userJson['data'][0]['is_confirm'] ==false){

      Get.to(RealNameAuthenPage());
      BotToast.showText(text: '检测到你还没有实名认证,考试前请先进行实名认证');
    }

    update();

  }

  /// 修改接口
  void requestDataWithapi_my_write(var name,var tftext)async{

    var params ={
      'form-field-char-${name}':'${tftext}',
    };

    String result = await DioUtils(url:await PersistentStorage().getStorage('urlStr')).
    postModel(Address.getattt,model: 'res.users',function_name: '_api_my_write',params:params );
    Map json = jsonDecode(result);

    BotToast.showText(text: json['message']);

    // Get.back();

    update();

  }

  /// 上传头像
  // void UpdataDataWithHeader(var userAvatar) async {
  //   String result = await DioUtils(
  //       url: await PersistentStorage().getStorage('urlStr'))
  //       .postModel(
  //       '/api/v1/write/${await PersistentStorage().getStorage('partner_id')}',
  //       model: 'res.partner',
  //       params: {'form-field-char-image_128': userAvatar});
  //   Map json = jsonDecode(result);
  //
  //   update();
  // }



  Future openCamera() async {
    var image = await ImagePicker.pickImage(
        source: ImageSource.camera, imageQuality: 50);

    return image;
  }

  Future openGallery() async {
    var image = await ImagePicker.pickImage(
        source: ImageSource.gallery, imageQuality: 50);

    print('sdfsdfsd ${image}');

    return image;
  }

  @override
  void onInit() {

    requestDataWithUserInfo();
    requestDataWithUser();
    // TODO: implement onInit
    print('onInit 执行了一次');
    super.onInit();
  }
  @override
  void onReady() {
    // TODO: implement onReady

    print('onReady 执行了一次');
    super.onReady();
  }

}

class RenZhenInfoModel {
  String datetime;
  bool cache;
  List<Data> data;
  int errcode;
  String errmsg;
  String message;

  RenZhenInfoModel(
      {this.datetime,
        this.cache,
        this.data,
        this.errcode,
        this.errmsg,
        this.message});

  RenZhenInfoModel.fromJson(Map<String, dynamic> json) {
    datetime = json['datetime'];
    cache = json['cache'];
    if (json['data'] != null) {
      data = new List<Data>();
      json['data'].forEach((v) {
        data.add(new Data.fromJson(v));
      });
    }
    errcode = json['errcode'];
    errmsg = json['errmsg'];
    message = json['message'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['datetime'] = this.datetime;
    data['cache'] = this.cache;
    if (this.data != null) {
      data['data'] = this.data.map((v) => v.toJson()).toList();
    }
    data['errcode'] = this.errcode;
    data['errmsg'] = this.errmsg;
    data['message'] = this.message;
    return data;
  }
}

class Data {
  String name;
  String fullName;
  String sfz;
  String sfzHeads;
  String sfzTails;
  String aiFace;
  String exam_project_name;
  String exam_company_name;



  Data(
      {this.name,
        this.exam_project_name,
        this.exam_company_name,
        this.fullName,
        this.sfz,
        this.sfzHeads,
        this.sfzTails,
        this.aiFace});

  Data.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    fullName = json['full_name'];
    sfz = json['sfz'];
    sfzHeads = json['sfz_heads'];
    sfzTails = json['sfz_tails'];
    aiFace = json['ai_face'];
    exam_project_name = json['exam_project_name'];
    exam_company_name = json['exam_company_name'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['full_name'] = this.fullName;
    data['sfz'] = this.sfz;
    data['sfz_heads'] = this.sfzHeads;
    data['sfz_tails'] = this.sfzTails;
    data['ai_face'] = this.aiFace;
    return data;
  }
}
